#include "Factory.h"
#include <iostream>

vector<Object*> Factory::m_objectList; //static variables need to be explicityly added to one cpp

void Factory::DeleteObject(Object *o)
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		if (m_objectList[i] == o)
		{
			m_objectList[i] = NULL;
			delete o;
			return;
		}
	}
}

void Factory::AddObject(Object *o)
{
	m_objectList.push_back(o);
}

void Factory::RenderAll()
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		m_objectList[i]->Render();
	}
}

void Factory::UpdateAll()
{
	for (int i = 0; i < m_objectList.size(); ++i)
	{
		m_objectList[i]->Update();
	}
	m_objectList.erase(std::remove(m_objectList.begin(), m_objectList.end(),(Object*)NULL), m_objectList.end());
}

void Factory::DoCollisions()
{
	for (unsigned int a = 0; a < m_objectList.size(); ++a)
	{
		for (unsigned int b = 0; b < m_objectList.size(); ++b)
		{
			if (a != b)
			{
				if (CollisionCheck(m_objectList[a]->r, m_objectList[b]->r))
				{
					//collision stuff
					backgroundSpeed = 0;
				}
			}
		}
	}
}

//Chris
bool Factory::CollisionCheck(sf::FloatRect a, sf::FloatRect b)
{
	if (a.left > b.left + b.width) // check if left of object a is greater than the right of object a
	{
		return false;
	}

	if (a.left + a.width < b.left) // check if left of object b is greater than the right of object a
	{
		return false;
	}
		
	if (a.top > b.top + b.height) // check if top of object a is less than the bottom of object b
	{
		return false;
	}
		
	if (a.top + a.height < b.top) // check if top of object b is less than the bottom of object a
	{
		return false;
	}

	return true; // If none of the above are true, return true. As the above check for whether a collision is at all possible, if all are false, then a collision is taking place 
}
// 

// Josh
float Factory::GetDistance(sf::Sprite &a, sf::Sprite &b)
{
	float dist = a.getPosition().x - b.getPosition().x;

	if (dist < 130.0f)
	{
		backgroundSpeed = 0;
		caughtTiger = true;
	}

	return dist;
}
//

